var dateType = '1',  //日期类型
    dateStart = '', //开始日期
    dateEnd = '',   //结束日期
	leftListID = '';
//用户选择
function selectSH(id) {
	var te = $("#SH").attr("btnType");
	if(te == "隐藏") {
		$("#SH").attr("btnType", "显示");
		$("#selectBox").fadeIn(600, function() {
			$("#selectBox").css("display", "block")
		})
	}
	if(te == "显示") {
		$("#SH").attr("btnType", "隐藏");
		$("#selectBox").fadeOut(600, function() {
			$("#selectBox").css("display", "none")
		})
	}
}
//用户选择结束


//设置session参数
function setSessionfunc(typeid,zid,tankuang,danwei){
	
	//设置当前用户的session值
    //typeid 系统分类id (比如对应前端八大类), zid 当前用户的站点id
	var url="/bigscrenn/setUserSession?typeid="+typeid+"&zid="+zid+"&cid="+tankuang;
    $.ajax({
	     cache: true,
	     type: "POST",
	     url:url,
	     data:"",
	     async: false,				   
	     success: function(data){
              console.log(data,'参数');         
	     }
    		
    });	
};
var userid = "";   //用户id
var unitID = "";   //单位id
var userType = ""; //用户类型
var kehu = [];     //左边列表
var zhandian = []; //站点数组
var zid = "";     //站点id
var zName='';      //站点名称
var danweiID='';    //单位id
var danweiName='';   //单位名称
//获取session 开始
getSession();
function getSession(){
	
	var url="/bigscrenn/getUserSession";
     $.ajax({
	     cache: true,
	     type: "POST",
	     url:url,
	     data:"",
	     async: false,				   
	     success: function(data){
	    	 //alert("session数据：="+data);
	    	 //js解析json串
     	     var obj = JSON.parse(data);
             if (obj.session.length>0){
                   /*alert(obj.session[0].UserID);//用户id
                   alert(obj.session[0].Name);//用户名称
                   alert(obj.session[0].UnitID);//用户单位id
                   alert(obj.session[0].UserAuthority);//用户权限 2 所有单位 3 本单位
                   alert(obj.session[0].typeid);//系统分类id (比如对应前端八大类)
                   alert(obj.session[0].zid);//当前用户的站点id*/			            	   
            	   userid = obj.session[0].UserID;
 	  			   unitID = obj.session[0].UnitID;
 	  			   userType = obj.session[0].UserAuthority;
 	  			   //zid = obj.session[0].zid;
 	  			   
 	  			   danweiID = (obj.session[0].cid1).split(",")[0];  //单位id方法
 	  			   danweiName = (obj.session[0].cid1).split(",")[1];//上页选中来的单位名称
 	  			   
 	  			   //vs.zid = obj.session[0].zid;
 	  			   zid = (obj.session[0].zid).split(",")[0];  //站点id
 	  			   zName = (obj.session[0].zid).split(",")[1];//上页选中来的站点名称
 	  			   //alert("历史曲线 danweiID="+danweiID + " danweiName= "+danweiName +"zid = "+zid +"zName = "+zName);
 	  			   $('#topTitle').text(zName);
 	  			   
 	  			   zhandianid = zid;
 	  			   onlineFunc();
 	  			   
 	  			   console.log("站点id="+zid);
 	  			   //setSessionfunc('',vs.zid+','+vs.zName,'',(vs.danweiID+','+vs.danweiName));     // session存站点（第二个参）
 	  			   
 	  			   
 	  			   if(zid==undefined){
 	  				zid = obj.session[0].zid;
 	  			   }
 	  			   //vs.getData(userid,zid);
             }
                 //alert("zid= "+zid+"userid="+userid+"and unitID="+unitID+"and userType="+userType) ;
                 //alert("zid = "+zid);
                
                if(zid && zid != '' && zid != null && zid!= undefined){
                	//vs.getUnitIData(userid,unitID);
                }
                //具体用户进来的操作
                if(userType==3){
                	console.log("我的身份是具体用户");
                	$(".backHome-box >a").css("display","none");// “返回首页”隐藏
                }
	     }
     });
}
//获取session 结束

//站点列表方法开始
setTimeout(getUnitIData,500);
function getUnitIData(){
	//alert("历史曲线 danweiID="+danweiID +" usurid = "+ userid+" danweiID = "+danweiID + " danweiName= "+danweiName +"zid = "+zid +"zName = "+zName);
	// userid用户id,unitid单位id,unitid为空时表示调用多个单位的站点信息
    var url="/bigscrenn/getSiteList?userid="+userid+"&unitid="+danweiID;
     $.ajax({
	     cache: true,
	     type: "POST",
	     url:url,
	     data:"",
	     async: false,				   
	     success: function(data){
           // alert(data);
           // js解析json串
           var obj = JSON.parse(data);
           // alert(obj.total);//记录数
           zhandian = obj.rows;// 分类后的站点（用户）
           
           //====
           var zd=obj.rows;
           for(var i = 0;i<zhandian.length;i++){
        	   	$("#zhanDianDl").append('<dd><a class="zhanDian" cid='+obj.rows[i].CID+'>'+obj.rows[i].UNITNAME+'</a></dd>')
           }
           //====
           
           // unitData.getPicData(obj.rows[0].CID);//电气图ID（进入页面时默认第一个站点的电气图）
           // alert("========="+obj.rows[0].CID);
           
    	   var arrr = $(".zhanDian");
    	   // alert("arrr=="+arrr.length);
    	   var firstACID = $(".zhanDian:first()").attr("cid");
    	   localStorage.setItem("energyCID", firstACID);// 没有点击存储本页第一个的站点CID覆盖homecid
    	   var thisCid="";
    	   for (var n=0;n<arrr.length;n++){
    		   arrr[n].index = n;
    		   arrr[n].onclick = function(){
    			   // alert($(this).text()+" 站点id = "+$(this).attr("cid"));
    			   thisCid = $(this).attr("cid");
    			   //setSessionfunc('',thisCid);                   //session存参
    			   setSessionfunc('',thisCid+','+$(this).text(),'',(danweiID+','+danweiName));     // session存站点（第二个参）
    			   window.location.href = "./specificUser.html";// 返回具体页面
    		   }
    		   
    	   }
           
	     }
     });
}
//站点列表方法结束


//实时曲线左侧列表开始
setTimeout(getData,530);
function getData() {
//	alert("历史曲线 ---左侧 danweiID="+danweiID +" usurid = "+ userid+" danweiID = "+danweiID + " danweiName= "+danweiName +"zid = "+zid +"zName = "+zName);
	
	//得到指定站点柱型图分析选择列表
    //参数unitid站点id
    var url="/bigscrenn/getColumnarList?unitid="+zid;
    //alert("柱型图分析选择列表 url ="+url);
     $.ajax({
	     cache: true,
	     type: "POST",
	     url:url,
	     data:"",
	     async: false,			   
	     success: function(data){
           //alert("柱型图分析 data= "+data);
           //js解析json串
           var obj = JSON.parse(data);
           
           for (var i=0;i<obj.rows.length;i++){
             //alert(obj.rows[i].ID);//ID
             //alert(obj.rows[i].NAME);//名称
        	   $("#dl").append('<dd><a id='+obj.rows[i].ID+' href="javascript:void(0);">&nbsp;<span>'+obj.rows[i].NAME+'</span></a></dd>');  
           }
           $("#dl>dd:first() a").addClass("sideActive");
		   var defaultUnitid = $("#dl>dd a.sideActive").attr("id");
		  //alert("defaultUnitid = "+defaultUnitid);
		   leftListID = defaultUnitid;
		   
		   if($(".show >input").length==1){
				dateStart = $(".show input").val();
				dateEnd = '';
			}
			if($(".show >input").length==2){
				dateStart = $(".show input").eq(0).val();
				dateEnd = $(".show input").eq(1).val();
			}
		   test22();
		   $("#thisTitle").text($("#dl>dd a.sideActive").text());// 默认选中第一对应标题
		   //列表 点击事件
		   layui.use('element', function() {
			    var element = layui.element; // 导航的hover效果、二级菜单等功能，需要依赖element模块
			    element.on('nav(demo)', function(elem) {
				 // console.log(elem)
				//alert(elem.text());
				$("#thisTitle").text(elem.text()); // 选中选项对应标题
				$("#dl>dd a").removeClass("sideActive");
				
				$(this).addClass("sideActive");
				leftListID = $(this).attr("id");
				dateType = $('.xz').find('input').attr('dateType');
				//alert("leftListID==="+leftListID+" dateType = "+dateType);
				test22();
			    })
		   })
	     }
     });
	
}
//实时曲线左侧列表结束

var dateType=1;
//日期时间查询结束
function func(){
	
	leftListID = $(".sideActive").attr('id');
	
	if($(".show >input").length==1){
		dateStart = $(".show input").val();
		dateEnd = '';
	}
	if($(".show >input").length==2){
		dateStart = $(".show input").eq(0).val();
		dateEnd = $(".show input").eq(1).val();
	}
	dateType = $('.xz').find('input').attr('datetype');
	
	test22();
}
//今日，上周，上个月，近三个月 开始
// 得到图表接口
// unitid是左边菜单的ID
// sdate 需要查询的开始时间
// edate 需要查询的结束时间
// type 类型 1，24小时，2 30天，3 12月，4 4季度，5年
$(function() {
	
	$(".am-radio-inline").removeClass('xz');
	$(".am-radio-inline").eq(0).addClass('xz');
	secondLiTab = $(".am-radio-inline");
	secondLiTab.click(function() {
		var index = secondLiTab.index(this);
		$(".am-radio-inline").removeClass('xz');
		$(".am-radio-inline").eq(index).addClass('xz');
		
		 leftListID = $(".sideActive").attr('id');
		//alert("leftListID = "+leftListID);
		
		//监测点击事件单选事件
		if($(".show >input").length==1){
			dateStart = $(".show input").val();
			dateEnd = '';
		}
		if($(".show >input").length==2){
			dateStart = $(".show input").eq(0).val();
			dateEnd = $(".show input").eq(1).val();
		}
		
		dateType = $(this).find('input').attr('dateType');
		
		
		//alert("dateStart = "+dateStart + " dateEnd = " + dateEnd + " dateType = " + dateType + " leftListID = " + leftListID);
		//var url ="/bigtongji/getChartSetupListRelist?unitid=000000000001&sdate=2018-12-13&edate=2018-12-13&type="+lsqxType;
		
		var url = "/bigtongji/getChartSetupListRelist?unitid="+leftListID+"&sdate="+dateStart+"&edate="+dateEnd+"&type="+dateType;	
		//alert("点击后历史曲线的地址======"+url);
		test22();
	});
})

var valueArr1=[],valueArr2=[],valueArr3=[],Xarr=[],legenArr=[];
//得到指定站点柱型图分析图表
//test22();

function test22(){
	//alert("Xarr === "+Xarr);
	if(Xarr.length>0){
		Xarr.length = 0;
	 }
	if(valueArr1.length>0){
		valueArr1.length = 0;
	 }
	if(valueArr2.length>0){
		valueArr2.length = 0;
	 }
	if(valueArr3.length>0){
		valueArr3.length = 0;
	 }
	//alert("leftListID = "+leftListID+" dateStart = "+dateStart+" dateEnd = "+dateEnd + " dateType = "+dateType);
    //参数id选择的配置id,type统计类别 1 按日统计2 按月统计 3 按年度统计 4 按多年度统计,sdate统计日期1,edate统计日期2
    //var url="/bigscrenn/getColumnarTJ?id=A00000000041&type=1&sdate=2019-04-05&edate=";
    var url="/bigscrenn/getColumnarTJ?id="+leftListID+"&type="+dateType+"&sdate="+dateStart+"&edate="+dateEnd;
    //alert("柱型图分析图表 url = "+url);
    $.ajax({
	     cache: true,
	     type: "POST",
	     url:url,
	     data:"",
	     async: false,				   
	     success: function(data){
               //alert("柱型图分析图表data = "+data);
               //js解析json串
               if(data!=''&&data!=undefined){
            	   var obj = JSON.parse(data); 
                   for (var i=0;i<obj.rows.length;i++){
                         //alert(obj.rows[i].ITEM);//项目
                         
                         var data1 = obj.rows[i].DATA.split(",");
                         
                         //alert("最大值："+data1[0]);//最大值
                         //alert("最小值："+data1[1]);//最小值
                         //alert("平均值："+data1[2]);//平均值
                         
                         Xarr.push(obj.rows[i].ITEM);
                         valueArr1.push(data1[0]);
                         valueArr2.push(data1[1]);
                         valueArr3.push(data1[2]);
                         
                   }
                    //alert("Xarr 1= "+Xarr);
                    //alert("valueArr1 = "+valueArr1);
                   
               }
               historyFunc();
	     }
     });
     
}

//echart表 开始

//function historyFunc(dataA, dataB, dataC,Xarr,legenArr) {
function historyFunc() {
	//alert("Xarr = "+Xarr);
	if(Xarr!='' && Xarr != undefined){
		$("#tbfx-bar").show();
	
	//function jianFengPinGuFunc(arr1, arr2, arr3, arr4,shijian,title) {
		//近两个月的能耗比 开始
		
		//if(title=="undefined" || title==null){title='Kwh'}
		var latelyEnergyChart = echarts.init(document.getElementById("tbfx-bar"));
		latelyEnergy_option = {
			backgroundColor: 'transparent',
			//backgroundColor: 'rgb(0, 35, 120, .36)',
			title: {
				show:false,
				text: '单位',
				//text: '单位('+title+')',
				x: '2%',
				y:"5%",
				textStyle: {
					//文字颜色
					color: '#fff',
					//字体风格,'normal','italic','oblique'
					fontStyle: 'normal',
					//字体粗细 'normal','bold','bolder','lighter',100 | 200 | 300 | 400...
					fontWeight: '100',
					//字体系列
					fontFamily: 'sans-serif',
					//字体大小
					fontSize: 18
				}
			},
			tooltip: {
				trigger: 'axis',
				axisPointer: {
					type: 'shadow'
				}
			},
			legend: {
				data: ['最大值', '最小值'],
				align: 'left',
				top: '5%',
				right:'5%',
				textStyle: {
					color: "#fff",
					fontSize: 16
				},
				itemWidth: 30,
				itemHeight: 20,
				itemGap: 25
			},
			grid: {
				left: '5%',
				top: '20%',
				right: '5%',
				bottom: '15%',
				containLabel: true
			},
			xAxis: [{
				type: 'category',
				data: Xarr,
				data1: [
					'1',
					'2',
					'3',
					'4',
					'5',
					'6',
					'7',
					'8',
					'9',
					'10',
					'11',
					'12',
					'13',
					'14',
					'15',
					'16',
					'17',
					'18',
					'19',
					'20',
					'21',
					'22',
					'23',
					'24'
				],
				axisLine: {
					show: true,
					lineStyle: {
						color: "rgba(255,255,255,.1)",
						width: 1,
						type: "solid"
					},
				},
				axisTick: {
					show: false,
				},
				axisLabel: {
					interval: 0, 
					//formatter: '{value} 点',
					formatter: '{value}',
					// rotate:50,
					show: true,
					splitNumber: 3,
					textStyle: {
						fontFamily: "微软雅黑",
						color: "rgba(255,255,255,.6)",
						fontSize: '120%',
					},
				},
			}],
			yAxis: [{
				type: 'value',
				axisLabel: {
					formatter: '{value}',
					show: true,
					textStyle: {
						fontFamily: "微软雅黑",
						color: "rgba(255,255,255,.6)",
						fontSize: '120%',
					},
				},
				axisTick: {
					show: false,
				},
				axisLine: {
					show: true,
					lineStyle: {
						color: "rgba(255,255,255,.1	)",
						width: 1,
						type: "solid"
					},
				},
				splitLine: {
					lineStyle: {
						color: "rgba(255,255,255,.1)",
					}
				}
			}],
			series: [{
				name: '最大值',
				type: 'bar',
				data: valueArr1,
				//data:[12,21,1,23,4,3],
				barWidth: '18%', //柱子宽度
				// barGap: 1, //柱子之间间距

				itemStyle: {
					normal: {
						color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
							offset: 0,
							color: '#3ca1ff'
						}, {
							offset: 1,
							color: '#0a73d5'
						}]),
						opacity: 1,
						barBorderRadius: 3,
					}
				}
			}, {
				name:  '最小值',
				type: 'bar',
				//data:[2,21,1,13,14,13,23,11,13,23,43],
				data: valueArr2,
				barWidth: '18%',
				// barGap: 1,
				itemStyle: {
					normal: {
						color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
							offset: 0,
							color: '#DE67F4'
						}, {
							offset: 1,
							color: '#1CDEE0'
						}]),
						opacity: 1,
						barBorderRadius: 3,
					}
				}
			},]
		};
		latelyEnergyChart.setOption(latelyEnergy_option,true);
		$(document).ready(function() {
			latelyEnergyChart.resize();
		});
		window.addEventListener("resize", function() {
			latelyEnergyChart.resize();
		})
		
	}else{
		$("#tbfx-bar").hide()
	}
	
}

//echart表 结束

//值班人员
//onlineFunc();
function onlineFunc(){
	//alert("online zhandianid = "+zhandianid);
	// unitid 是站点ID
	// var url="/bigtongji/getRotaList?unitid=000000000001";
	var url="/bigtongji/getRotaList?unitid="+zhandianid;
	 $.ajax({
	     cache: true,
	     type: "POST",
	     url:url,
	     data:"",
	     async: false,				   
	     success: function(data){
        // alert(data);
        console.log(data);
        var dataObj=eval("("+data+")");// 转换为json对象
        // alert(dataObj);
        // alert(dataObj);
        if(dataObj.rows.length>0){
    		 $("#onlineNotice").html('');
    		 for (var i = 0; i < dataObj.rows.length; i++) {
         		var cid= dataObj.rows[i].cid;
         		var name= dataObj.rows[i].rname;
         		var phone= dataObj.rows[i].phoneCode;
         		//alert(cid);
         		//alert("cid="+cid+"name="+name+"phone="+phone);
        		 $("#onlineNotice").append('<li><span>👨‍⚖️&nbsp;&nbsp;<span id="onlineName1">'+name+'</span>&nbsp;电话：</span><span>'+phone+'</span></li>');
         	  }
    	  }
    	 if(dataObj.rows.length==0){
    		$("#onlineNotice").html(''); 
    	 }
	     }
	 });

}
//值班人员结束

//返回上一级跳转逻辑处理
function bachFunc(){
	var urlsStr = localStorage.getItem("urls");
	var ss = JSON.parse(urlsStr);
	if(ss.sanji != undefined &&  ss.sanji !=''){
    	//alert(ss.sanji);
	    window.location.href = './specificHome.html';
    	return;
    }
    if(ss.erji != undefined && ss.erji !=''){
    	//alert(ss.erji);
	    window.location.href = './intlgMonitor.html';
	    return;
    }if(ss.yiji != undefined &&  ss.yiji !=''){
    	//alert(ss.yiji);
	    window.location.href = './pro_Home.html';
	    return;
    }else{
	   //alert(222)
    }
}

